﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.Core;
using GrayParrot.Core.Data;
using Test.DAL;

namespace Dashboard.BL.Facades
{
    public class CustomersFacade : FacadeBase<CustomersFacade>
    {
        NorthWindContext m_ctx = null;

        public CustomersFacade()
        {
        }

        public override void Initialize(IDbContext context)
        {
               
        }

        public override void Initialize()
        {
            m_ctx = new NorthWindContext();
            m_ctx.Configuration.LazyLoadingEnabled = false;
            m_ctx.Configuration.ProxyCreationEnabled = false;
        }

        public override void Dispose()
        {
            if (m_ctx != null)
            {
                m_ctx.Dispose();
            }
        }

        public ReturnValue<List<string>> GetCompanies()
        {
            ReturnValue<List<string>> retValue = new ReturnValue<List<string>>();
            retValue.HasValue = false;
            retValue.Value = null;

            try
            {
                var objs = (from x in m_ctx.Customers
                            select x.CompanyName).Distinct();

                if (objs.Any())
                {
                    retValue.Value = objs.ToList();
                    retValue.HasValue = true;
                }
            }
            catch (Exception ex)
            {
                retValue.HasError = true;
                retValue.Error = ex;
            }

            return retValue;
        }
    }
}
